import 'package:flutter/widgets.dart';
import 'package:go_router/go_router.dart';
import 'package:todo_app/routes.dart';

class AppNavigator {
  static BuildContext get context {
    return rootNavigatorState.currentContext!;
  }

  static void toLoginPage({String? from}) {
    return pushReplacement('/user/login', extra: {from});
  }

  /// Get a location from route name and parameters.
  static String namedLocation(
    String name, {
    Map<String, String> params = const <String, String>{},
    Map<String, dynamic> queryParams = const <String, dynamic>{},
  }) =>
      GoRouter.of(context)
          .namedLocation(name, pathParameters: params, queryParameters: queryParams);

  /// Navigate to a location.
  static void go(String location, {Object? extra}) =>
      GoRouter.of(context).go(location, extra: extra);

  /// Navigate to a named route.
  static void goNamed(
    String name, {
    Map<String, String> params = const <String, String>{},
    Map<String, dynamic> queryParams = const <String, dynamic>{},
    Object? extra,
  }) =>
      GoRouter.of(context).goNamed(
        name,
        pathParameters: params,
        queryParameters: queryParams,
        extra: extra,
      );

  /// Push a location onto the page stack.
  static void push(String location, {Object? extra}) =>
      GoRouter.of(context).push(location, extra: extra);

  /// Navigate to a named route onto the page stack.
  static void pushNamed(
    String name, {
    Map<String, String> params = const <String, String>{},
    Map<String, dynamic> queryParams = const <String, dynamic>{},
    Object? extra,
  }) =>
      GoRouter.of(context).pushNamed(
        name,
        pathParameters: params,
        queryParameters: queryParams,
        extra: extra,
      );

  /// Returns `true` if there is more than 1 page on the stack.
  static bool canPop() => GoRouter.of(context).canPop();

  /// Pop the top page off the Navigator's page stack by calling
  /// [Navigator.pop].
  static void pop<T extends Object?>([T? result]) => GoRouter.of(context).pop(result);

  /// Replaces the top-most page of the page stack with the given URL location
  /// w/ optional query parameters, e.g. `/family/f2/person/p1?color=blue`.
  ///
  /// See also:
  /// * [go] which navigates to the location.
  /// * [push] which pushes the location onto the page stack.
  static void pushReplacement(String location, {Object? extra}) =>
      GoRouter.of(context).pushReplacement(location, extra: extra);

  /// Replaces the top-most page of the page stack with the named route w/
  /// optional parameters, e.g. `name='person', params={'fid': 'f2', 'pid':
  /// 'p1'}`.
  ///
  /// See also:
  /// * [goNamed] which navigates a named route.
  /// * [pushNamed] which pushes a named route onto the page stack.
  static void pushReplacementNamed(
    String name, {
    Map<String, String> params = const <String, String>{},
    Map<String, dynamic> queryParams = const <String, dynamic>{},
    Object? extra,
  }) =>
      GoRouter.of(context).pushReplacementNamed(
        name,
        pathParameters: params,
        queryParameters: queryParams,
        extra: extra,
      );
}